/*
 * Copyright (c) 2017. 深圳一七网络科技. All rights reserved.
 */

package com.fenxiangle.yueding.feature.order.presenter;

import android.text.TextUtils;

import com.amap.api.maps.model.Text;
import com.amap.api.services.core.PoiItem;
import com.fenxiangle.yueding.entity.bo.OrderBo;
import com.fenxiangle.yueding.entity.bo.Page2Bo;
import com.fenxiangle.yueding.entity.bo.PageBo;
import com.fenxiangle.yueding.entity.bo.PublishBo;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.entity.bo.TeamBo;
import com.fenxiangle.yueding.entity.bo.UserImageBo;
import com.fenxiangle.yueding.feature.order.contract.OrderContract;
import com.fenxiangle.yueding.feature.order.dependencies.order.DaggerOrderPresenterComponent;
import com.fenxiangle.yueding.feature.publish.contract.PublishContract;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.DaggerPublishPresenterComponent;
import com.suozhang.framework.entity.bo.Result;

import org.json.JSONObject;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class OrderPresenter implements OrderContract.Presenter {

    private OrderContract.View mView;


    @Inject

    OrderContract.Model mModel;


    public OrderPresenter(OrderContract.View mView) {
        this.mView = mView;
        DaggerOrderPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void confirmOrder(OrderBo orderBo) {
        mModel.confrimOrder(orderBo)
                .compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String couponBoPageData) {
                if (couponBoPageData != null) {
                    mView.confirmOrder(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void appeal(OrderBo orderBo) {
        mModel.appeal(orderBo)
                .compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String couponBoPageData) {
                if (couponBoPageData != null) {
                    mView.showAppealTxSuccess(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void sheet(String orderId) {
        OrderBo pageBo = new OrderBo(orderId);
        mModel.sheet(pageBo)
                .compose(mView.bindToLife()).subscribe(new Observer<UserImageBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserImageBo couponBoPageData) {
                if (couponBoPageData != null) {
                    mView.showSheetSuccess(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void cancelSheet(String orderId) {
        PageBo pageBo = new PageBo(orderId);
        mModel.cancelSheet(pageBo)
                .compose(mView.bindToLife()).subscribe(new Observer<UserImageBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserImageBo couponBoPageData) {
                if (couponBoPageData != null) {
                    mView.showcancleSheetSuccess(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void sheetList(int pageNow, int pageSize) {
        PageBo pageBo = new PageBo(pageNow, pageSize);
        mModel.sheetList(pageBo)
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                if (couponBoPageData != null && couponBoPageData.size() > 0) {
                    mView.showSheetListSuccess(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getPublishInviteList(int pageNow, int pageSize, Integer type) {
        PageBo pageBo = new PageBo(pageNow, pageSize);
        pageBo.setType(type);
        mModel.getReceiveList(pageBo)
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                if (couponBoPageData != null && couponBoPageData.size() > 0) {
                    mView.showPublishListSuccess(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getPublishInviteListMore(int pageNow, int pageSize, Integer type) {
        PageBo pageBo = new PageBo(pageNow, pageSize);
        pageBo.setType(type);
        mModel.getReceiveList(pageBo)
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                mView.showPublishListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getOrderList(int pageNow, int pageSize, String orderStatus) {
        Page2Bo pageBo = new Page2Bo();
        pageBo.setPageNow(pageNow);
        pageBo.setPageSize(pageSize);
        pageBo.setOrderStatus(orderStatus);
        mModel.getOrderList(pageBo)
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                if (couponBoPageData != null && couponBoPageData.size() > 0) {
                    mView.showPublishListSuccess(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getOrderListMore(int pageNow, int pageSize, String orderStatus) {
        Page2Bo pageBo = new Page2Bo();
        pageBo.setPageNow(pageNow);
        pageBo.setPageSize(pageSize);
        pageBo.setOrderStatus(orderStatus);
        mModel.getOrderList(pageBo)
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                mView.showPublishListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void acceptOrder(String orderId) {
        if (TextUtils.isEmpty(orderId)) {
            return;
        }
        mModel.acceptOrder(new OrderBo(orderId))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showAcceptSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void refuseOrder(String orderId) {
        if (TextUtils.isEmpty(orderId)) {
            return;
        }
        mModel.refuseOrder(new OrderBo(orderId))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showRefuseSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void txOrder(String orderId) {
        mModel.txOrder(new OrderBo(orderId)).compose(mView.bindToLife()).subscribe(new Observer<PublishBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(PublishBo s) {
                mView.dismissLoading();
                mView.showConfrimTxSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void cancleOrder(String orderId, String type) {
        if (TextUtils.isEmpty(orderId)) {
            return;
        }
        mModel.cancleOrder(new OrderBo(orderId, type))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showCancleSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toComment(JSONObject jsonObject) {
        mModel.toComment(jsonObject)
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.toComments(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getUserList(int pageIndex, int pageSize, String userUid, String orderId, int type) {
        mModel.getUserList(new PageBo(pageIndex, pageSize, userUid, orderId, type))
                .compose(mView.bindToLife())
                .subscribe(new Observer<List<TeamBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(List<TeamBo> s) {
                        if (s != null && s.size() > 0) {
                            mView.showUserListSuccess(s);
                        } else {
                            mView.showPublishListEmpty();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
